'Copyright (c) 2008, Jonathan Allen
Imports ClrExtensions.Net.Rest

Public MustInherit Class Queue
    Implements IEnumerable(Of QueueItem)

    Private m_User As User
    Private m_ETag As String
    Private m_Items As New List(Of QueueItem)
    Private m_LastRefresh As Date?

    Public Property ETag() As String
        Get
            Return m_ETag
        End Get
        Protected Set(ByVal value As String)
            m_ETag = value
            m_LastRefresh = Now
        End Set
    End Property

    Public Sub New(ByVal user As User)
        m_User = user
    End Sub

    Public ReadOnly Property LastRefresh() As Date?
        Get
            Return m_LastRefresh
        End Get
    End Property

    Public ReadOnly Property User() As User
        Get
            Return m_User
        End Get
    End Property

    Protected Friend ReadOnly Property ItemsCore() As List(Of QueueItem)
        Get
            Return m_Items
        End Get
    End Property

    Public Sub Add(ByVal title As CatalogTitle)
        Add(title, Nothing)
    End Sub

    Public Sub Add(ByVal title As CatalogTitle, ByVal position As Integer?)
        Add(title.Id, position)
    End Sub

    Public Sub Add(ByVal id As String)
        Add(id, Nothing)
    End Sub

    Public MustOverride Sub Add(ByVal id As String, ByVal position As Integer?)

    Public Sub Remove(ByVal title As QueueItem)
        Remove(title.Id)
    End Sub

    Public Sub Remove(ByVal position As Integer)
        For Each entry In m_Items
            If entry.Position = position Then
                Remove(entry)
            End If
        Next
    End Sub

    Public Sub Remove(ByVal id As String)
        If LastRefresh Is Nothing Then Refresh()

        Dim request = New RestCall(RestVerb.Delete, id)
        Dim xml = User.Connection.RequestResource(request, User)
        'Console.WriteLine(Xml)

        'TODO - Learn to parse the result so we don't have to refresh
        Refresh()
    End Sub

    Public MustOverride Sub Refresh()

    ''' <summary>
    ''' Determines which item is in the specific index value of the user's Netflix Queue.
    ''' </summary>
    ''' <param name="index">
    ''' The index of the Queue item to return.
    ''' </param>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' The QueueItem at the index position in the user's Netflix Queue.
    ''' </returns>
    ''' <remarks>
    ''' This searches by Index, not by position.
    ''' </remarks>
    Default Public ReadOnly Property Item(ByVal index As Integer) As QueueItem
        Get
            If LastRefresh Is Nothing Then Refresh()
            Return ItemsCore(index)
        End Get
    End Property

    ''' <summary>
    ''' Searches through the user's Netflix Queue to determine which item is in the indicated position.
    ''' </summary>
    ''' <param name="position">
    ''' Numeric position to check for a QueueItem.
    ''' </param>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' The QueueItem in the requested position if a QueueItem exists;
    ''' A Null if no QueueItem exists in the requested position.
    ''' </returns>
    ''' <remarks>
    ''' 
    ''' </remarks>
    Public ReadOnly Property ItemByPosition(ByVal position As Integer) As QueueItem
        Get
            If LastRefresh Is Nothing Then Refresh()
            For Each entry In ItemsCore
                If entry.Position = position Then Return entry
            Next
            Return Nothing
        End Get
    End Property

    ''' <summary>
    ''' Provides the current Count of items in the user's Netflix Queue.
    ''' </summary>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' Integer count of items currently in the user's Netflix Queue.
    ''' </returns>
    ''' <remarks>
    ''' Refreshes the Refresh() variable every time this Property is accessed.
    ''' </remarks>
    Public ReadOnly Property Count() As Integer
        Get
            If LastRefresh Is Nothing Then Refresh()
            Return ItemsCore.Count
        End Get
    End Property

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of QueueItem) Implements System.Collections.Generic.IEnumerable(Of QueueItem).GetEnumerator
        If LastRefresh Is Nothing Then Refresh()
        Return ItemsCore.GetEnumerator
    End Function

    Private Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        If LastRefresh Is Nothing Then Refresh()
        Return ItemsCore.GetEnumerator
    End Function
End Class
